package com.zwps.common.controller.config;

import com.zwps.common.controller.filter.ContentCachingRequestFilter;
import com.zwps.common.controller.interceptor.APILogInterceptor;
import com.zwps.common.controller.interceptor.LoginInfoContextClearInterceptor;
import com.zwps.common.core.config.CoreConfig;

import cn.hutool.core.date.DatePattern;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Configuration
public class CustomWebMvcConfig implements WebMvcConfigurer {

    /**
     * LocalDate转换器，用于转换RequestParam和PathVariable参数
     */
    @Bean
    public Converter<String, LocalDate> localDateConverter() {
        return new Converter<String, LocalDate>() {

            @Override
            public LocalDate convert(String source) {
                return LocalDate.parse(source);
            }
        };
    }

    /**
     * LocalDateTime转换器，用于转换RequestParam和PathVariable参数
     */
    @Bean
    public Converter<String, LocalDateTime> localDateTimeConverter() {
        return new Converter<String, LocalDateTime>() {

            @Override
            public LocalDateTime convert(String source) {
                return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
            }
        };
    }

    @Bean
    public LoginInfoContextClearInterceptor loginInfoContextClearInterceptor() {
        return new LoginInfoContextClearInterceptor();
    }
    
    @Bean
    public ContentCachingRequestFilter contentCachingRequestFilter() {
        return new ContentCachingRequestFilter();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInfoContextClearInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new APILogInterceptor()).addPathPatterns("/**");
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(localDateConverter());
        registry.addConverter(localDateTimeConverter());
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (HttpMessageConverter<?> httpMessageConverter : converters) {
            if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) {
                // 在原来的序列化器对象替换自定义的
                ((MappingJackson2HttpMessageConverter) httpMessageConverter).setObjectMapper(CoreConfig.createObjectMapper());
            }
        }
    }
}
